import javax.swing.JFrame;


public class RadarUpdater implements Runnable{
	
	Main parentView;
	JFrame subView;
	static final int alignTimer = 20; 		  // .02 second updates
	static final int updateTimer = 1000*5*60; // every 5 minutes
	public long lastAlign;
	public long lastUpdate;
	public boolean animate;
	volatile boolean hide;
	public long dx;
	public long time;
	public boolean fastSlider;
	
	
	public RadarUpdater(Main $parentView, JFrame $subView, boolean fast){
		
		parentView = $parentView;
		subView = $subView;
		
		lastAlign = System.currentTimeMillis();
		lastUpdate = System.currentTimeMillis();
		
		animate = true;
		time = System.currentTimeMillis();
		
		if(fast)
			dx = 5;
		else
			dx = 0;
		
		fastSlider = fast;
	}

	@Override
	public void run() {
		
		while(animate){
			
			try {
				
				if(fastSlider)
					animate();
				else
					animateSlow();
				
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		
		
		while(subView.isVisible() && !hide){
			
			if(System.currentTimeMillis() - lastAlign > alignTimer){
				align();
			}
			
			if(System.currentTimeMillis() - lastUpdate > updateTimer){
				renewMap();
			}
			
			//Thread.sleep(50);
		}
		
		
	}
	
	public void renewMap(){
		
	}

	public void align(){
		if(subView != null){
			if(parentView.getLocationOnScreen().x+parentView.getWidth() != subView.getLocationOnScreen().x ||
					parentView.getLocationOnScreen().y != subView.getLocationOnScreen().y	){
				subView.setLocation(parentView.getLocationOnScreen().x+parentView.getWidth(),parentView.getLocationOnScreen().y);
				subView.setVisible(true);
			}
		}
	}
	
	public void animate() throws InterruptedException{
		
		long ddx = - ((System.currentTimeMillis()- time)/10000) * 10 + 10;
		dx = dx + ddx;
		if(subView.getLocation().x >= parentView.getLocationOnScreen().x + parentView.getWidth()){
			animate = false;
		}else {
			
			subView.setLocation((int) (parentView.getLocationOnScreen().x+parentView.getWidth()-subView.getWidth()+dx),parentView.getLocationOnScreen().y);
						
			Thread.sleep(20);
		}
		
	}
	
	public void animateSlow() throws InterruptedException{
		if(subView.getLocation().x >= parentView.getLocationOnScreen().x + parentView.getWidth()){
			animate = false;
		}else {
			
			subView.setLocation((int) (parentView.getLocationOnScreen().x+parentView.getWidth()-subView.getWidth()+dx),parentView.getLocationOnScreen().y);
			dx += 2;			
			Thread.sleep(20);
		}
		
	}
}
